/* Copyright 2008. All rights reserved. */
package org.bling.parser.tags;

import org.bling.parser.BlingParserException;
import org.bling.parser.expressions.ExpressionManager;
import org.bling.parser.style.StyleParser;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Widget;

/**
 * renders an SWT Label
 * 
 * @author DISBROB
 * 
 */
public class LabelTag extends Tag {

	protected Label label;
	protected String style;

	public void setStyle(String style) {
		this.style = style;
	}
	
	@Override
	public int doStartTag() throws BlingParserException {
		super.doStartTag();

		if (!(parent.getWidget() instanceof Composite)) {
			throw new BlingParserException(
					"Label tags can only be inside of composites");
		}
		Composite composite = (Composite) parent.getWidget();
		label = new Label(composite, SWT.WRAP);

		// Change the default height of a label to 19, to match 'Text' tag
		Point size = label.getSize();
		label.setSize(size.x, 19);

		StyleParser.applyStyle((Control) getWidget(), style);
		
		return EVAL_PAGE;
	}

	@Override
	public int doAfterBody() throws BlingParserException {
		super.doAfterBody();
		String evalBody = ExpressionManager.evaluate(bodyContent, context).toString();
		label.setText(evalBody);
		return EVAL_PAGE;
	}

	@Override
	public Widget getWidget() throws BlingParserException {
		return label;
	}

}
